home *** CD-ROM | disk | FTP | other *** search
- /*
- File: HeapTools.h
-
- Copyright: © 1991-1994 by Apple Computer, Inc.
- All rights reserved.
-
- Part of the AOCE Sample SMSAM Package. Consult the license
- which came with this software for your specific legal rights.
-
- */
-
-
-
- #ifndef __HEAPTOOLS__
- #define __HEAPTOOLS__
-
- #ifndef __MEMORY__
- #include "Memory.h"
- #endif
-
- #ifndef __OSUTILS__
- #include "OSUtils.h"
- #endif
-
- #ifndef __FILES__
- #include "Files.h"
- #endif
-
- class ostream;
-
- #pragma push
- #pragma segment HeapTools
-
- /***********************************|****************************************/
-
- struct BlockHeader24
- {
- unsigned fType : 2;
- unsigned fUnused : 2;
- unsigned fCorrection : 4;
- unsigned fPhysicalSize : 24;
-
- union {
- unsigned fRelativeHandle;
- Zone* fZone;
- } u;
- };
-
- /***********************************|****************************************/
-
- struct BlockHeader32
- {
- unsigned fType : 2;
- unsigned fUnused : 6;
- unsigned fLocked : 1;
- unsigned fPurgeable : 1;
- unsigned fResource : 1;
- unsigned fReserved : 13;
- unsigned fCorrection : 8;
- unsigned fPhysicalSize;
-
- union {
- unsigned fRelativeHandle;
- Zone* fZone;
- } u;
- };
-
- /***********************************|****************************************/
-
- class BlockInfo
- {
- public: BlockInfo ();
- BlockInfo ( const Zone* );
- BlockInfo ( const BlockInfo& );
- ~BlockInfo ();
-
- BlockInfo& operator = ( const BlockInfo& );
- Boolean operator == ( register const BlockInfo& ) const;
- Boolean operator != ( const BlockInfo& ) const;
-
- enum BlockType { kFree, kNonrelocatable, kRelocatable };
-
- void Set ( const void* blockHeader );
-
- BlockType GetType () const;
-
- unsigned long GetLogicalSize () const;
- const void* GetLogicalStart () const;
-
- unsigned long GetPhysicalSize () const;
- const void* GetPhysicalStart () const;
-
- unsigned long BlockHeaderSize () const;
- const void* GetIdentifierAddress () const;
-
- void SetMarked ( Boolean = true );
- Boolean IsMarked () const;
-
- OSErr Write ( short RefNum ) const;
- OSErr Read ( short RefNum );
-
- ostream& operator >> ( ostream& ) const;
-
- private: ostream& Stream24 ( ostream& ) const;
- ostream& Stream32 ( ostream& ) const;
- void* NextBlockHeader () const;
-
- const Zone* fZone;
- const void* fStart;
- Boolean fMode32;
- Boolean fMarked;
-
- union {
- BlockHeader24 f24;
- BlockHeader32 f32;
- } fHeader;
-
- friend class THeapWalker;
- };
-
- /***********************************|****************************************/
-
- class THeapWalker
- {
- public: THeapWalker ( const Zone* zone );
- virtual ~THeapWalker ();
-
- void Reset ();
- Boolean Next ();
- const BlockInfo& GetCurrent ();
-
- private: THeapWalker ( const THeapWalker& );
- THeapWalker& operator = ( const THeapWalker& );
-
- const Zone* fZone;
- const void* fStart;
- const void* fEnd;
- const void* fCurrent;
- BlockInfo fBlock;
- };
-
- /***********************************|****************************************/
-
- class THeapState
- {
- public: THeapState ();
- THeapState ( const Zone* );
- THeapState ( const FSSpec& );
- THeapState ( const THeapState& );
- ~THeapState ();
-
- THeapState& operator = ( const THeapState& );
- Boolean operator == ( const THeapState& ) const;
- Boolean operator != ( const THeapState& ) const;
-
- THeapState& operator -= ( THeapState& );
- THeapState& operator &= ( THeapState& );
- THeapState& operator ^= ( THeapState& );
-
- unsigned long BlockCount () const;
- unsigned long HeapSize () const;
- const BlockInfo& operator [] ( unsigned long zeroBasedIndex ) const;
- Boolean Contains ( const BlockInfo& ) const;
-
- void MarkAllBlocks ( Boolean = true );
- void UnmarkAllBlocks ();
- unsigned long MarkedBlocksCount () const;
- unsigned long MarkedBlocksSize () const;
-
- OSErr Dump (); // calls StdPutFile
- OSErr Dump ( const StringPtr prompt ); // calls StdPutFile
- OSErr Dump ( const FSSpec& );
-
- OSErr Load (); // calls StdGetFile
- OSErr Load ( const FSSpec& );
-
- ostream& operator >> ( ostream& ) const;
-
- protected: OSErr Copy ( const Zone* );
- OSErr Copy ( const THeapState& );
-
- private: unsigned long fCount;
- BlockInfo* fBlocks;
- };
-
- /***********************************|****************************************/
-
- inline ostream& operator << ( ostream& s, const BlockInfo& i ) { return i >> s; }
- inline ostream& operator << ( ostream& s, const THeapState& i ) { return i >> s; }
- inline unsigned long BlockInfo::BlockHeaderSize () const { return fMode32 ? 12 : 8; }
- inline unsigned long BlockInfo::GetPhysicalSize () const { return fMode32 ? fHeader.f32.fPhysicalSize : fHeader.f24.fPhysicalSize; }
- inline const void* BlockInfo::GetPhysicalStart () const { return fStart; }
- inline unsigned long BlockInfo::GetLogicalSize () const { return fMode32 ? fHeader.f32.fPhysicalSize - fHeader.f32.fCorrection - 12 : fHeader.f24.fPhysicalSize - fHeader.f24.fCorrection - 8; }
- inline void* BlockInfo::NextBlockHeader () const { return (void*) ( (unsigned long) fStart + GetPhysicalSize () ); }
- inline void BlockInfo::SetMarked ( Boolean marked ) { fMarked = marked; }
- inline Boolean BlockInfo::IsMarked () const { return fMarked; }
- inline unsigned long THeapState::BlockCount () const { return fCount; }
- inline Boolean BlockInfo::operator != ( const BlockInfo& that ) const { return !operator == ( that ); }
- inline BlockInfo::BlockType BlockInfo::GetType () const { return (BlockType) fHeader.f32.fType; }
-
- #pragma pop
-
- /***********************************|****************************************/
-
- #endif // __HEAPTOOLS__
-